/*
 ============================================================================
 Name        : hev-config.c
 Author      : Heiher <admin@heiher.info>
 Version     : 0.0.2
 Copyright   : Copyright (c) 2010 everyone.
 Description : Linkage 802.1x for Unix-like operating system.
 ============================================================================
 */

#include "hev-config.h"

/*#define HEV_CONFIG_PATH						"."G_DIR_SEPARATOR_S"config.ini"*/
#define HEV_CONFIG_NAME                                         "/etc/linkage-hev.ini"

#define HEV_CONFIG_GENERAL					"General"
#define HEV_CONFIG_GENERAL_AUTORETRY		"AutoRetry"
#define HEV_CONFIG_GENERAL_AUTORETRY_DEF	TRUE
#define HEV_CONFIG_GENERAL_RETRYNUMBER		"RetryNumber"
#define HEV_CONFIG_GENERAL_RETRYNUMBER_DEF	3
#define HEV_CONFIG_GENERAL_INTERFACE		"Interface"
#define HEV_CONFIG_GENERAL_INTERFACE_DEF	"eth0"

#define HEV_CONFIG_ACCOUNT					"Account"
#define HEV_CONFIG_ACCOUNT_NAME				"Name"
#define HEV_CONFIG_ACCOUNT_NAME_DEF			"name"
#define HEV_CONFIG_ACCOUNT_PASSWD			"Passwd"
#define HEV_CONFIG_ACCOUNT_PASSWD_DEF		"passwd"

#define HEV_CONFIG_GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE((obj), HEV_TYPE_CONFIG, HevConfigPrivate))

typedef struct _HevConfigPrivate HevConfigPrivate;

struct _HevConfigPrivate
{
	GKeyFile * key_file;
};

static void hev_config_initable_iface_init(GInitableIface * iface);
static gboolean hev_config_initable_init(GInitable * initable,
					GCancellable * cancellable, GError ** error);

G_DEFINE_TYPE_WITH_CODE(HevConfig, hev_config, G_TYPE_OBJECT,
		G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, hev_config_initable_iface_init));

static void hev_config_dispose(GObject * obj)
{
	HevConfig * self = HEV_CONFIG(obj);
	HevConfigPrivate * priv = HEV_CONFIG_GET_PRIVATE(self);

	/* keyfile START */
	if(priv->key_file)
	{
		g_key_file_free(priv->key_file);

		priv->key_file = NULL;
	}
	/* keyfile END */

	G_OBJECT_CLASS(hev_config_parent_class)->dispose(obj);
}

static void hev_config_finalize(GObject * obj)
{
	HevConfig * self = HEV_CONFIG(obj);
	HevConfigPrivate * priv = HEV_CONFIG_GET_PRIVATE(self);

	G_OBJECT_CLASS(hev_config_parent_class)->finalize(obj);
}

static GObject * hev_config_constructor(GType type, guint n, GObjectConstructParam * param)
{
	return G_OBJECT_CLASS(hev_config_parent_class)->constructor(type, n, param);
}

static void hev_config_constructed(GObject * obj)
{
}

static void hev_config_class_init(HevConfigClass * klass)
{
	GObjectClass * obj_class = G_OBJECT_CLASS(klass);

	obj_class->constructor = hev_config_constructor;
	obj_class->constructed = hev_config_constructed;
	obj_class->dispose = hev_config_dispose;
	obj_class->finalize = hev_config_finalize;

	g_type_class_add_private(klass, sizeof(HevConfigPrivate));
}

static void hev_config_initable_iface_init(GInitableIface * iface)
{
	iface->init = hev_config_initable_init;
}

static void hev_config_init(HevConfig * self)
{
	HevConfigPrivate * priv = HEV_CONFIG_GET_PRIVATE(self);

	priv->key_file = NULL;
}

static gboolean hev_config_initable_init(GInitable * initable,
					GCancellable * cancellable, GError ** error)
{
	HevConfig * self = HEV_CONFIG(initable);
	HevConfigPrivate * priv = HEV_CONFIG_GET_PRIVATE(self);

	/* keyfile START */
	{
		priv->key_file = g_key_file_new();
		/*g_key_file_load_from_file(priv->key_file, HEV_CONFIG_PATH, G_KEY_FILE_NONE, NULL);*/
		g_key_file_load_from_file(priv->key_file, HEV_CONFIG_NAME, G_KEY_FILE_NONE, NULL);
	}
	/* keyfile END */

	/* default START */
	{
		/* General -> AutoRetry */
		if(!g_key_file_has_key(priv->key_file, HEV_CONFIG_GENERAL, HEV_CONFIG_GENERAL_AUTORETRY, NULL))
		{
			g_key_file_set_boolean(priv->key_file, HEV_CONFIG_GENERAL,
					HEV_CONFIG_GENERAL_AUTORETRY, HEV_CONFIG_GENERAL_AUTORETRY_DEF);
		}
		/* General -> RetryNumber */
		if(!g_key_file_has_key(priv->key_file, HEV_CONFIG_GENERAL, HEV_CONFIG_GENERAL_RETRYNUMBER, NULL))
		{
			g_key_file_set_integer(priv->key_file, HEV_CONFIG_GENERAL,
					HEV_CONFIG_GENERAL_RETRYNUMBER, HEV_CONFIG_GENERAL_RETRYNUMBER_DEF);
		}
		/* General -> Interface */
		if(!g_key_file_has_key(priv->key_file, HEV_CONFIG_GENERAL, HEV_CONFIG_GENERAL_INTERFACE, NULL))
		{
			g_key_file_set_string(priv->key_file, HEV_CONFIG_GENERAL,
					HEV_CONFIG_GENERAL_INTERFACE, HEV_CONFIG_GENERAL_INTERFACE_DEF);
		}
		/* Account -> Name */
		if(!g_key_file_has_key(priv->key_file, HEV_CONFIG_ACCOUNT, HEV_CONFIG_ACCOUNT_NAME, NULL))
		{
			g_key_file_set_string(priv->key_file, HEV_CONFIG_ACCOUNT,
					HEV_CONFIG_ACCOUNT_NAME, HEV_CONFIG_ACCOUNT_NAME_DEF);
		}
		/* Account -> Passwd */
		if(!g_key_file_has_key(priv->key_file, HEV_CONFIG_ACCOUNT, HEV_CONFIG_ACCOUNT_PASSWD, NULL))
		{
			g_key_file_set_string(priv->key_file, HEV_CONFIG_ACCOUNT,
					HEV_CONFIG_ACCOUNT_PASSWD, HEV_CONFIG_ACCOUNT_PASSWD_DEF);
		}
	}
	/* default END */

	return TRUE;
}

GObject * hev_config_new(GCancellable * cancellable, GError ** error)
{
	return g_initable_new(HEV_TYPE_CONFIG, cancellable, error, NULL);
}

gboolean hev_config_get_general_autoretry(HevConfig * self)
{
	HevConfigPrivate * priv = NULL;

	g_return_val_if_fail(HEV_IS_CONFIG(self), FALSE);

	priv = HEV_CONFIG_GET_PRIVATE(self);

	return g_key_file_get_boolean(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_AUTORETRY, NULL);
}

gint hev_config_get_general_retrynumber(HevConfig * self)
{
	HevConfigPrivate * priv = NULL;

	g_return_val_if_fail(HEV_IS_CONFIG(self), -1);

	priv = HEV_CONFIG_GET_PRIVATE(self);

	return g_key_file_get_integer(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_RETRYNUMBER, NULL);
}

gchar * hev_config_get_general_interface(HevConfig * self)
{
	HevConfigPrivate * priv = NULL;

	g_return_val_if_fail(HEV_IS_CONFIG(self), NULL);

	priv = HEV_CONFIG_GET_PRIVATE(self);

	return g_key_file_get_string(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_INTERFACE, NULL);
}

gchar * hev_config_get_account_name(HevConfig * self)
{
	HevConfigPrivate * priv = NULL;

	g_return_val_if_fail(HEV_IS_CONFIG(self), NULL);

	priv = HEV_CONFIG_GET_PRIVATE(self);

	return g_key_file_get_string(priv->key_file, HEV_CONFIG_ACCOUNT,
			HEV_CONFIG_ACCOUNT_NAME, NULL);
}

gchar * hev_config_get_account_passwd(HevConfig * self)
{
	HevConfigPrivate * priv = NULL;

	g_return_val_if_fail(HEV_IS_CONFIG(self), NULL);

	priv = HEV_CONFIG_GET_PRIVATE(self);

	return g_key_file_get_string(priv->key_file, HEV_CONFIG_ACCOUNT,
			HEV_CONFIG_ACCOUNT_PASSWD, NULL);
}

void hev_config_set_general_autoretry(HevConfig * self, gboolean retry)
{
	HevConfigPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_CONFIG(self));

	priv = HEV_CONFIG_GET_PRIVATE(self);

	g_key_file_set_boolean(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_AUTORETRY, retry);
}

void hev_config_set_general_retrynumber(HevConfig * self, gint number)
{
	HevConfigPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_CONFIG(self));

	priv = HEV_CONFIG_GET_PRIVATE(self);

	g_key_file_set_integer(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_RETRYNUMBER, number);
}

void hev_config_set_general_interface(HevConfig * self, const gchar * name)
{
	HevConfigPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_CONFIG(self));

	priv = HEV_CONFIG_GET_PRIVATE(self);

	g_key_file_set_string(priv->key_file, HEV_CONFIG_GENERAL,
			HEV_CONFIG_GENERAL_INTERFACE, name);
}

void hev_config_set_account_name(HevConfig * self, const gchar * name)
{
	HevConfigPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_CONFIG(self));

	priv = HEV_CONFIG_GET_PRIVATE(self);

	g_key_file_set_string(priv->key_file, HEV_CONFIG_ACCOUNT,
			HEV_CONFIG_ACCOUNT_NAME, name);
}

void hev_config_set_account_passwd(HevConfig * self, const gchar * passwd)
{
	HevConfigPrivate * priv = NULL;

	g_return_if_fail(HEV_IS_CONFIG(self));

	priv = HEV_CONFIG_GET_PRIVATE(self);

	g_key_file_set_string(priv->key_file, HEV_CONFIG_ACCOUNT,
			HEV_CONFIG_ACCOUNT_PASSWD, passwd);
}

